package com.liuzhou.action.subtask;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.SeekableByteChannel;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.liuzhou.dao.ItemDao;
import com.liuzhou.dao.SubTaskDao;
import com.liuzhou.dao.impl.ItemDaoImpl;
import com.liuzhou.dao.impl.SubtaskDaoImpl;

import net.sf.json.JSONObject;

public class SubtaskUpdateAction extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		System.out.println("进入修改子任务的的过程");
		HttpSession session = req.getSession();
		String pid = (String)session.getAttribute("updataId");		
		Integer id = Integer.parseInt(pid);
		System.out.println("UpdateAction获取到的id: [" + id + "]");
		
		// 获取页面数据
		String subtaskName = req.getParameter("subtask_name");
		String subtaskDetail = req.getParameter("subtask_detail");
		String subtaskItemName = req.getParameter("subtask_item_name");
		System.out.println("UpdateAction获取到的Name: [" + subtaskName + "]"
				+"Detail:["+subtaskDetail+"]"
				+" 父事项ItemName:["+subtaskItemName+"]");
		// 从dao中想数据库更新一条数据
		try {

			ItemDao itemDao = new ItemDaoImpl();
			Integer item_id = itemDao.qureyIdByName(subtaskItemName);// TODO 暂不保证父事项不重名
			System.out.println("查询到的父事项id: "+item_id);
			SubTaskDao subTaskDao = new SubtaskDaoImpl();
			subTaskDao.update(id ,item_id, subtaskName, subtaskDetail);//子任务插入完成

		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 向前端页面响应
		// 响应
		JSONObject json = new JSONObject();
		json.put("statusCode", "200");
		json.put("message", "OK!");
		json.put("navTabId", "productAddPage");
		json.put("rel", "productedList");
		json.put("callbackType", "closeCurrent");

		PrintWriter out = resp.getWriter();
		out.println(JSONObject.fromObject(json));
		out.flush();
		out.close();
	}

}
